﻿using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;

public class BunkerHitPointArea : MonoBehaviour
{
    public bool isShowBound = true;
    public BunkerHitPointType type = BunkerHitPointType.Metal;
    [HideInInspector]    
    public int luaType;
    public float radius = 0.3f;
    public float randomRange = 0.1f;
    [HideInInspector]
    public List<Vector3> positions = new List<Vector3>();
    [HideInInspector]
    public List<Vector3> directions = new List<Vector3>();
    private Vector3[] boundDefaultPositions = new Vector3[4]{new Vector3(-1,1,0),new Vector3(1,1,0),new Vector3(1,-1,0),new Vector3(-1,-1,0) };
    public GameObject[] bounds;
    
    
    public void CreateBounds()
    {
        if (bounds == null)
        {
            bounds = new GameObject[4];
            for (int i = 0; i < bounds.Length; i++)
            {
                bounds[i] = new GameObject("point"+i);
                bounds[i].transform.SetParent(transform);
                bounds[i].transform.localPosition = boundDefaultPositions[i];
            }
        }
    }

    private float PointToLine(Vector3 p, Vector3 p1, Vector3 p2)
    {
        float p2pDistance = Vector3.Distance(p2, p);
        Vector3 p2p1 = p2 - p1;
        Vector3 p2p = p2 - p;
        float dot = Vector3.Dot(p2p1, p2p);
        float rad = Mathf.Acos(dot / (p2p1.magnitude * p2pDistance));
        float distance = p2pDistance * Mathf.Sin(rad);
        return distance;
    }

    public void AutoCreateBunkerPoints()
    {
        positions.Clear();
        directions.Clear();
        
        //分为两个三角形
        var a = bounds[0].transform.position;
        var c = bounds[1].transform.position;
        var b = bounds[2].transform.position;
        

        var height = PointToLine(c,a,b);
        var length = Vector3.Distance(a, b);
        for (float h = radius; h <= height - radius; h += radius * 2)
        {
            var start = a + h / height * (c - a);
            var l = (height - h) / height * length;
            for (float dd = radius; dd <= l - radius; dd += radius * 2)
            {
                positions.Add(start + (b - a).normalized * dd + UnityEngine.Random.insideUnitSphere * randomRange);
                directions.Add(Vector3.forward);
            }
        }
        
        c = bounds[3].transform.position;
        height = PointToLine(c,a,b);
        length = Vector3.Distance(a, b);
        for (float h = radius; h <= height - radius; h += radius * 2)
        {
            var start = a + h / height * (c - a);
            var l = (height - h) / height * length;
            for (float dd = radius; dd <= l - radius; dd += radius * 2)
            {
                positions.Add(start + (b - a).normalized * dd + UnityEngine.Random.insideUnitSphere * randomRange);
                directions.Add(Vector3.forward);
            }
        }

        BunkerHitPoint.AutoFix(transform, positions, directions);
        
    }
#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        if (bounds != null && isShowBound)
        {
            for (int i = 0; i < bounds.Length; i++)
            {
                Gizmos.DrawLine(bounds[i].transform.position,bounds[(i+1)%bounds.Length].transform.position);
            }
        }
        for (int i = 0; i < positions.Count; i++)
        {
            Gizmos.DrawSphere(positions[i],radius / 2);
            var direction = Vector3.forward;
            if (directions.Count > i)
            {
                direction = directions[i];
            }
            Gizmos.DrawLine(positions[i],positions[i] + direction * 0.3f);
        }
    }
#endif
}
